package Communication;



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;




















import Controller.SystemController;
import Entity.*;
import HandleDB.CustomerQuery;
import HandleDB.MySqlConnection;
import HandleDB.WorkerQuery;



public class Server extends AbstractServer {
	private String userName = null;
	private String password = null;
	final public static int DEFAULT_PORT = 5555;
	
	Message back = null;
	ResultSet rs = null;
	String str = null;
	int flag = 0;

	private MySqlConnection mySQL = null;

	public Server(int port) {
		super(port);
		mySQL = new MySqlConnection();

	}




	@Override
	protected void handleMessageFromClient(Object message, ConnectionToClient client){
		
		Message msg;
		if(flag==0){
			flag=1;
			
		}
		else{
			msg = (Message)message;
		
		
		

					switch (msg.getMsg()) {

					case "AddNewCustomer":
						 addNewCustomer(msg);
						break;
					case "GetUser":
						 getUser((Worker)msg.getObj());
						break;
						
					}
		}
	}

	
	private void getUser(Worker worker)  {
		try {
			
			Worker workerTmp = WorkerQuery.getRow( , worker.getusername(), worker.getPassword());
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void addNewCustomer(Message msg)  {
	//	Customer customer= (Customer) msg.getObj();
		Customer customer= new Customer();
		customer.setAge(1956);
		customer.setCfirstName("hila");
		customer.setCity("haifa");
		
		
		try {
			CustomerQuery.insert(customer);
		} catch (Exception e) {
			System.out.println("Server -> addnewcustomer"+ e.getMessage());
			e.printStackTrace();
		}
	}

	public void SearchCustomer(ResultSet rs, String str)
	{
		try {
			while (rs.next()) {
				
				Customer Cst = new Customer(rs.getString(1), rs.getString(2),
						rs.getInt(3),rs.getString(4) ,rs.getString(5),
						rs.getString(6));
				
				back = new Message(Cst,MessageHandle.SEARCH_CUSTOMER, true);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void SearchProduct(ResultSet rs, String str)
	{
		try {
			while (rs.next()) {
				
				Product Pct = new Product();
				Pct.setProductID(rs.getInt(1));
				Pct.setProductName(rs.getString(2));
				Pct.setPrice(rs.getFloat(3));
				//Pct.setPicture(rs.getURL(4));
				Pct.setDescription(rs.getString(5));
				
				back = new MessageHandle(Pct,MessageHandle.SEARCH_PRODUCT, true);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void SearchPattern(ResultSet rs, String str)
	{
		try {
			while (rs.next()) {
				
				Pattern Ptr = new Pattern();
				Ptr.setPatternID(rs.getInt(1));
				Ptr.setPatternName(rs.getString(2));
				Ptr.setMessage(rs.getString(3));
				Ptr.setProductID(rs.getInt(4));
				
				back = new MessageHandle(Ptr,MessageHandle.SEARCH_PATTERN, true);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public void SearchReport(ResultSet rs, String str)
	{
		try {
			while (rs.next()) {
				
				Report Rpt = new Report();
				Rpt.setReportId(rs.getInt(1));
				Rpt.setReportType(rs.getBoolean(2));
				Rpt.setReportDate(rs.getDate(3));
							
				back = new MessageHandle(Rpt,MessageHandle.SEARCH_REPORT, true);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	protected void serverStarted() {
		System.out.println("Server listening for connections on port "
				+ getPort());
	}

	protected void serverStopped() {
		System.out.println("Server has stopped listening for connections.");
	}

	public static void main(String[] args) {

		int port = 0; // Port to listen on

		try {
			//port = DEFAULT_PORT; // Set port to 5555
			port = Integer.parseInt(args[0]); // Get port from command line
		} catch (Throwable t) {
			port = DEFAULT_PORT; // Set port to 5555
		}

		Server server = new Server(port);

		try {
			server.listen(); // Start listening for connections
		} catch (Exception ex) {
			System.out.println("ERROR - Could not listen for clients!");
		}
	}

}
